package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.CustomerException;
import com.example.dao.categoryDao;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.Setmeal;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<categoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> DishWrapper = new LambdaQueryWrapper<Dish>();

        LambdaQueryWrapper<Setmeal> SetmealWrapper = new LambdaQueryWrapper<Setmeal>();

        DishWrapper.eq(Dish::getCategoryId, id);

        int count = dishService.count(DishWrapper);
        if(count>0){
            throw new CustomerException("该分类下有菜品，不能删除");
        }

        SetmealWrapper.eq(Setmeal::getCategoryId, id);

        count = setmealService.count(SetmealWrapper);

        if(count>0){
            throw new CustomerException("该分类下有套餐，不能删除");
        }

        super.removeById(id);
    }
}



